package com.sun.hunk.menu;

import com.sun.hunk.hibernate.dao.LinkDAO;
import com.sun.hunk.hibernate.dao.LinkTreeDAO;
import com.sun.hunk.hibernate.domain.Link;
import com.sun.hunk.hibernate.domain.LinkTree;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author sunil
 */
public class Menu implements ItemConstant{

    public Map<Link, Set<Link>> getMainMenu() {
        LinkTreeDAO dao = new LinkTreeDAO();
        return dao.getMainMenu();
    }

    public Link getItem(Long itemId) {
        LinkDAO dao = new LinkDAO();
        return dao.findById(itemId);
    }
    
    /**
     * This method will return the list of Items for bread crumb
     * @param itemId
     * @return
     */
    public List<Link> getBreadcrumb(String itemId) {
        if(itemId == null){
            return new ArrayList<Link>();
        }else{
            return getBreadcrumb(Long.parseLong(itemId));
        }
    }
    public List<Link> getBreadcrumb(Long itemId) {
        List<Link> list = new ArrayList<Link>();
        LinkDAO dao = new LinkDAO();
        Link mainItem = dao.findById(itemId);
        Link item = null;
        while ((item = dao.getParent(itemId)) != null) {
            list.add(item);
            itemId = item.getId();
        }
        list.add(mainItem);
        return list;
    }

    public List<MenuItem> getTabsTreeList(String parentId, String... options){
        List<LinkTree> list = getTabTreeList(Long.parseLong(parentId));
        List<MenuItem> menuItemlist = new ArrayList<MenuItem>();
        for(LinkTree item : list ){
            menuItemlist.add(new MenuItem(item.getChild()));
        }
        return menuItemlist;
    }

    private List<LinkTree> getTabTreeList(Long parentId) {
        LinkTreeDAO dao = new LinkTreeDAO();
        List<LinkTree> list = dao.getTabs(parentId);
        Collections.sort(list);
        return list;
    }
}
